package com.whiteape.book.core.manager;


import com.whiteape.book.core.page.PageMode;
import com.whiteape.book.core.page.PageStyle;
import com.whiteape.book.core.page.PageTextIntervalMode;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.utils.ScreenUtils;
import com.whiteape.book.utils.SharedPreUtils;

/**
 * Created by newbiechen on 17-5-17.
 * 阅读器的配置管理
 */

public class ReadSettingManager {
    /*************实在想不出什么好记的命名方式。。******************/
    public static final int READ_BG_DEFAULT = 0;
    public static final int READ_BG_1 = 1;
    public static final int READ_BG_2 = 2;
    public static final int READ_BG_3 = 3;
    public static final int READ_BG_4 = 4;
    public static final int NIGHT_MODE = 5;

    public static final String SHARED_READ_BG = "shared_read_bg";
    public static final String SHARED_READ_BRIGHTNESS = "shared_read_brightness";
    public static final String SHARED_READ_IS_BRIGHTNESS_AUTO = "shared_read_is_brightness_auto";
    public static final String SHARED_READ_TEXT_SIZE = "shared_read_text_size";
    public static final String SHARED_READ_IS_TEXT_DEFAULT = "shared_read_text_default";
    public static final String SHARED_READ_PAGE_MODE = "shared_read_mode";
    public static final String SHARED_READ_NIGHT_MODE = "shared_night_mode";
    public static final String SHARED_READ_VOLUME_TURN_PAGE = "shared_read_volume_turn_page";
    public static final String SHARED_READ_FULL_SCREEN = "shared_read_full_screen";
    public static final String SHARED_READ_CONVERT_TYPE = "shared_read_convert_type";
    public static final String SHARED_READ_TYPEFACE = "shared_read_typeface_file_path";
    public static final String SHARED_READ_SPEED = "shared_auto_read_speed";
    public static final String SHARED_LAST_SHARED_TIME = "shared_last_shared_time";
    public static final String SHARED_AGREE_PROTOCOL = "shared_agree_protocol";
    public static final String SHARED_SOURCE_VERSION_CODE = "shared_source_version_code";
    public static final String HOME_PAGE_SITE_HOST_URL = "homepage_site_host_url_";
    public static final String CATEGORY_INDEX_INITED = "category_index_inited";
    public static final String SHARED_READ_TEXT_USE_FANTI = "shared_read_text_use_fanti";
    public static final String SHARED_READ_TEXT_INTERVAL = "shared_read_text_interval";
    public static final String SHARED_READ_TEXT_BOLD = "shared_read_text_bold";

    private static volatile ReadSettingManager sInstance;

    private SharedPreUtils sharedPreUtils;

    public static ReadSettingManager getInstance() {
        if (sInstance == null) {
            synchronized (ReadSettingManager.class) {
                if (sInstance == null) {
                    sInstance = new ReadSettingManager();
                }
            }
        }
        return sInstance;
    }

    private ReadSettingManager() {
        sharedPreUtils = SharedPreUtils.getInstance();
    }

    public void setPageStyle(PageStyle pageStyle) {
        sharedPreUtils.putInt(SHARED_READ_BG, pageStyle.ordinal());
    }

    public void setBrightness(int progress) {
        sharedPreUtils.putInt(SHARED_READ_BRIGHTNESS, progress);
    }

    public void setAutoBrightness(boolean isAuto) {
        sharedPreUtils.putBoolean(SHARED_READ_IS_BRIGHTNESS_AUTO, isAuto);
    }

    public void setDefaultTextSize(boolean isDefault) {
        sharedPreUtils.putBoolean(SHARED_READ_IS_TEXT_DEFAULT, isDefault);
    }

    public void setTextSize(int textSize) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_SIZE, textSize);
    }

    public void setTypeface(String path) {
        sharedPreUtils.putString(SHARED_READ_TYPEFACE, path);
    }

    public String getTypeface() {
        return sharedPreUtils.getString(SHARED_READ_TYPEFACE);
    }

    public void setPageMode(PageMode mode) {
        sharedPreUtils.putInt(SHARED_READ_PAGE_MODE, mode.ordinal());
    }

    public void setNightMode(boolean isNight) {
        sharedPreUtils.putBoolean(SHARED_READ_NIGHT_MODE, isNight);
    }

    public int getBrightness() {
        return sharedPreUtils.getInt(SHARED_READ_BRIGHTNESS, 40);
    }

    public boolean isBrightnessAuto() {
        return sharedPreUtils.getBoolean(SHARED_READ_IS_BRIGHTNESS_AUTO, false);
    }

    public int getTextSize() {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_SIZE, ScreenUtils.spToPx(18));
    }

    public boolean isDefaultTextSize() {
        return sharedPreUtils.getBoolean(SHARED_READ_IS_TEXT_DEFAULT, false);
    }

    public PageMode getPageMode() {
        int mode = sharedPreUtils.getInt(SHARED_READ_PAGE_MODE, PageMode.SIMULATION.ordinal());
        return PageMode.values()[mode];
    }

    public PageStyle getPageStyle() {
        int style = sharedPreUtils.getInt(SHARED_READ_BG, PageStyle.BG_0.ordinal());
        return PageStyle.values()[style];
    }

    public boolean isNightMode() {
        return sharedPreUtils.getBoolean(SHARED_READ_NIGHT_MODE, false);
    }

    public void setVolumeTurnPage(boolean isTurn) {
        sharedPreUtils.putBoolean(SHARED_READ_VOLUME_TURN_PAGE, isTurn);
    }

    public boolean isVolumeTurnPage() {
        return sharedPreUtils.getBoolean(SHARED_READ_VOLUME_TURN_PAGE, false);
    }

    public void setFullScreen(boolean isFullScreen) {
        sharedPreUtils.putBoolean(SHARED_READ_FULL_SCREEN, isFullScreen);
    }

    public boolean isFullScreen() {
        return sharedPreUtils.getBoolean(SHARED_READ_FULL_SCREEN, false);
    }

    public void setConvertType(int convertType) {
        sharedPreUtils.putInt(SHARED_READ_CONVERT_TYPE, convertType);
    }

    public int getConvertType() {
        return sharedPreUtils.getInt(SHARED_READ_CONVERT_TYPE, 0);
    }

    public int getAutoReadSpeed() {
        return sharedPreUtils.getInt(SHARED_READ_SPEED, 5);
    }

    public void setAutoReadSpeed(int speed) {
        sharedPreUtils.putInt(SHARED_READ_SPEED, speed);
    }

    public void setLastSharedTime(long time) {
        sharedPreUtils.putLong(SHARED_LAST_SHARED_TIME, time);
    }

    public long getLastSharedTime() {
        return sharedPreUtils.getLong(SHARED_LAST_SHARED_TIME, 0L);
    }

    public boolean getIfAgreedProtocol() {
        return sharedPreUtils.getBoolean(SHARED_AGREE_PROTOCOL, false);
    }

    public void setIfAgreedProtocol(boolean agree) {
        sharedPreUtils.putBoolean(SHARED_AGREE_PROTOCOL, agree);

    }

    public int getSiteSourceVersionCode() {
        return sharedPreUtils.getInt(SHARED_SOURCE_VERSION_CODE, 0);
    }

    public void setSiteSourceVersionCode(int versionCode) {
        sharedPreUtils.putInt(SHARED_SOURCE_VERSION_CODE, versionCode);

    }

    public String getHomepageSiteHostUrl(int mediaType) {
        return sharedPreUtils.getString(HOME_PAGE_SITE_HOST_URL + mediaType);
    }

    public void setHomePageSiteHostUrl(int mediaType, String url) {
        sharedPreUtils.putString(HOME_PAGE_SITE_HOST_URL + mediaType, url);
    }

    public boolean getIfCategoryIndexInited() {
        return sharedPreUtils.getBoolean(CATEGORY_INDEX_INITED, false);
    }

    public void setCategoryIndexInited(boolean flag) {
        sharedPreUtils.putBoolean(CATEGORY_INDEX_INITED, flag);
    }

    public void setFanti(boolean isFanti) {
        sharedPreUtils.putBoolean(SHARED_READ_TEXT_USE_FANTI, isFanti);
    }

    public boolean getFanti() {
        return sharedPreUtils.getBoolean(SHARED_READ_TEXT_USE_FANTI, false);
    }

    public void setSharedReadTextInterval(int textIntervalType) {
        sharedPreUtils.putInt(SHARED_READ_TEXT_INTERVAL, textIntervalType);
    }

    public int getReadTextInterval() {
        return sharedPreUtils.getInt(SHARED_READ_TEXT_INTERVAL, PageTextIntervalMode.MODE_NORMAL);
    }

    public boolean getBold() {
        return sharedPreUtils.getBoolean(SHARED_READ_TEXT_BOLD, false);
    }

    public void setBold(boolean bold) {
        sharedPreUtils.putBoolean(SHARED_READ_TEXT_BOLD, bold);
    }
}
